﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Gmantis.Controls.Toolbar
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    public class ToolbarStripItem : ContentControl
    {
        // Fields
        private ContentPresenter cp;
        public static readonly DependencyProperty FocusBrushProperty = DependencyProperty.Register("FocusBrush", typeof(Brush), typeof(ToolbarStripItem), null);
        private Border hbdr;
        private Grid horizontalRoot;
        private bool isVertical;
        public static readonly DependencyProperty MouseOverBrushProperty = DependencyProperty.Register("MouseOverBrush", typeof(Brush), typeof(ToolbarStripItem), null);
        public static readonly DependencyProperty PressedBrushProperty = DependencyProperty.Register("PressedBrush", typeof(Brush), typeof(ToolbarStripItem), null);
        private string state;
        private Border vbdr;
        private Grid verticalRoot;

        // Methods
        public ToolbarStripItem()
        {
            base.DefaultStyleKey = typeof(ToolbarStripItem);
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            Size sz = finalSize;
            Border bdr = this.isVertical ? this.vbdr : this.hbdr;
            if ((((this.horizontalRoot != null) && (this.verticalRoot != null)) && ((this.hbdr != null) && (this.vbdr != null))) && ((sz.Width > 1.0) && (sz.Height > 1.0)))
            {
                if ((this.cp.Content is IToolbarControl) && !bdr.CornerRadius.IsEmpty())
                {
                    this.cp.Clip = Utils.ClipBorder(sz, bdr);
                }
                else
                {
                    this.cp.Clip = null;
                }
            }
            return base.ArrangeOverride(finalSize);
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            if (((this.horizontalRoot != null) && (this.verticalRoot != null)) && ((this.hbdr != null) && (this.vbdr != null)))
            {
                if (this.isVertical)
                {
                    this.horizontalRoot.Visibility = Visibility.Collapsed;
                    this.hbdr.Child = null;
                    this.vbdr.Child = this.cp;
                    this.verticalRoot.Visibility = Visibility.Visible;
                }
                else
                {
                    this.verticalRoot.Visibility = Visibility.Collapsed;
                    this.vbdr.Child = null;
                    this.hbdr.Child = this.cp;
                    this.horizontalRoot.Visibility = Visibility.Visible;
                }
            }
            if (!string.IsNullOrEmpty(this.InitialState))
            {
                VisualStateManager.GoToState(this, this.InitialState, false);
            }
            return base.MeasureOverride(availableSize);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.hbdr = base.GetTemplateChild("hbdr") as Border;
            this.vbdr = base.GetTemplateChild("vbdr") as Border;
            this.cp = base.GetTemplateChild("cp") as ContentPresenter;
            this.horizontalRoot = base.GetTemplateChild("HorizontalRoot") as Grid;
            this.verticalRoot = base.GetTemplateChild("VerticalRoot") as Grid;
        }

        internal void SetOrientation(Orientation orientation)
        {
            this.isVertical = orientation == Orientation.Vertical;
        }

        // Properties
        public Brush FocusBrush
        {
            get { return (Brush)GetValue(FocusBrushProperty); }
            set { SetValue(FocusBrushProperty, value); }
        }

        internal string InitialState
        {
            get { return this.state; }
            set { this.state = value; }
        }

        public Brush MouseOverBrush
        {
            get { return (Brush)GetValue(MouseOverBrushProperty); }
            set { SetValue(MouseOverBrushProperty, value); }
        }

        public Brush PressedBrush
        {
            get { return (Brush)GetValue(PressedBrushProperty); }
            set { SetValue(PressedBrushProperty, value); }
        }
    }
}
